package xhwl.hmo.mall.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

/**
 * Created by admin on 2017-11-10.
 */

public class AlarmUtil {
    private static final String TAG = "AlarmUtil";

    public static void controlAlarm(Context context, long triggerAtTime , int requestCode, Intent nextIntent) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
        }
        else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
        }
    }

    public static void cancelAlarm(Context context, String action,int requestCode) {
        Intent intent = new Intent(action);
        PendingIntent sender = PendingIntent.getBroadcast(
                context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // And cancel the alarm.
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }

}
